<?php
/**
 * Library of functions and constants for module evaluation
 * This file should have two well differenced parts:
 *   - All the core Moodle functions, neeeded to allow
 *     the module to work integrated in Moodle.
 *   - All the evaluation specific functions, needed
 *     to implement all the module logic. Please, note
 *     that, if the module become complex and this lib
 *     grows a lot, it's HIGHLY recommended to move all
 *     these module specific functions to a new php file,
 *     called "locallib.php" (see forum, quiz...). This will
 *     help to save some memory when Moodle is performing
 *     actions across all modules.
 *
 * Evaluation
 * By Hylson, Helton and Jackson
 * Rio do Sul - Santa Catarina - BRASIL
 */

/**
 * Given an object containing all the necessary data, 
 * (defined by the form in mod.html) this function 
 * will create a new instance and return the id number 
 * of the new instance.
 *
 * @param object $instance An object from the form in mod.html
 * @return int The id of the newly inserted evaluation record
 **/
function evaluation_add_instance($evaluation) {
    return insert_record("evaluation", $evaluation);
}

/**
 * Given an object containing all the necessary data, 
 * (defined by the form in mod.html) this function 
 * will update an existing instance with new data.
 *
 * @param object $instance An object from the form in mod.html
 * @return boolean Success/Fail
 **/
function evaluation_update_instance($evaluation) {
    $evaluation->id = $evaluation->instance;
    return update_record("evaluation", $evaluation);
}

/**
 * Given an ID of an instance of this module, 
 * this function will permanently delete the instance 
 * and any data that depends on it. 
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 **/
function evaluation_delete_instance($id) {

    if (! $evaluation = get_record("evaluation", "id", "$id")) {
        return false;
    }

    $result = true;

    # Delete any dependent records here #

    if (! delete_records("evaluation", "id", "$evaluation->id")) {
        $result = false;
    }

    return $result;
}

/**
 * Return a small object with summary information about what a 
 * user has done with a given particular instance of this module
 * Used for user activity reports.
 * $return->time = the time they did it
 * $return->info = a short text description
 *
 * @return null
 * @todo Finish documenting this function
 **/
function evaluation_user_outline($course, $user, $mod, $evaluation) {
    return $return;
}

/**
 * Print a detailed representation of what a user has done with 
 * a given particular instance of this module, for user activity reports.
 *
 * @return boolean
 * @todo Finish documenting this function
 **/
function evaluation_user_complete($course, $user, $mod, $evaluation) {
    return true;
}

/**
 * Given a course and a time, this module should find recent activity 
 * that has occurred in evaluation activities and print it out.
 * Return true if there was output, or false is there was none. 
 *
 * @uses $CFG
 * @return boolean
 * @todo Finish documenting this function
 **/
function evaluation_print_recent_activity($course, $isteacher, $timestart) {
    global $CFG;

    return false;  //  True if anything was printed, otherwise false 
}

/**
 * Function to be run periodically according to the moodle cron
 * This function searches for things that need to be done, such 
 * as sending out mail, toggling flags etc ... 
 *
 * @uses $CFG
 * @return boolean
 * @todo Finish documenting this function
 **/
function evaluation_cron () {
    global $CFG;

    return true;
}

/**
 * Must return an array of grades for a given instance of this module, 
 * indexed by user.  It also returns a maximum allowed grade.
 * 
 * Example:
 *    $return->grades = array of grades;
 *    $return->maxgrade = maximum allowed grade;
 *
 *    return $return;
 *
 * @param int $evaluationid ID of an instance of this module
 * @return mixed Null or object with an array of grades and with the maximum grade
 **/
function evaluation_grades($evaluationid) {
   return NULL;
}

/**
 * Must return an array of user records (all data) who are participants
 * for a given instance of evaluation. Must include every user involved
 * in the instance, independient of his role (student, teacher, admin...)
 * See other modules as example.
 *
 * @param int $evaluationid ID of an instance of this module
 * @return mixed boolean/array of students
 **/
function evaluation_get_participants($evaluationid) {
    return false;
}

/**
 * This function returns if a scale is being used by one evaluation
 * it it has support for grading and scales. Commented code should be
 * modified if necessary. See forum, glossary or journal modules
 * as reference.
 *
 * @param int $evaluationid ID of an instance of this module
 * @return mixed
 * @todo Finish documenting this function
 **/
function evaluation_scale_used ($evaluationid,$scaleid) {
    $return = false;
   
    return $return;
}

/**
 * Checks if scale is being used by any instance of evaluation.
 * This function was added in 1.9
 *
 * This is used to find out if scale used anywhere
 * @param $scaleid int
 * @return boolean True if the scale is used by any evaluation
 */
function evaluation_scale_used_anywhere($scaleid) {
    if ($scaleid and record_exists('evaluation', 'grade', -$scaleid)) {
        return true;
    } else {
        return false;
    }
}

/**
 * Execute post-install custom actions for the module
 * This function was added in 1.9
 *
 * @return boolean true if success, false on error
 */
function evaluation_install() {
     return true;
}

/**
 * Execute post-uninstall custom actions for the module
 * This function was added in 1.9
 *
 * @return boolean true if success, false on error
 */
function evaluation_uninstall() {
    return true;
}

//////////////////////////////////////////////////////////////////////////////////////
/// Any other evaluation functions go here.  Each of them must have a name that
/// starts with evaluation_
/// Remember (see note in first lines) that, if this section grows, it's HIGHLY
/// recommended to move all funcions below to a new "localib.php" file.

function evaluation_show_form($id) {

$s1 = get_string("dont_know", "evaluation");
$s2 = get_string("bad", "evaluation");
$s3 = get_string("regular", "evaluation");
$s4 = get_string("good", "evaluation");
$s5 = get_string("great", "evaluation");
$s6 = get_string("excelent", "evaluation");

$str_form = 
'<script javascript>'.
'function clica(valor)'.
'{'.
'  document.form_evaluate.nota[valor].checked=true;'.
'}'.
'</script>'.
'<link href="evaluation.css" rel=stylesheet type="text/css">'.
'<center><h3>'.
get_string("form_add_comment_title","evaluation").
'</h3></center>'.
'<form method=post action=insert_comment.php name=form_evaluate>'.
'<input type=hidden name=id value='.$id.'>'.
'<table align=center border=4 bordercolor=blue>'.
'  <tr bgcolor=yellow>'.
'    <td align=center>'.
get_string("form_add_comment_yourcomments","evaluation").
'<br>'.
'    <textarea name=comentario cols=60 rows=4></textarea></td>'.
'  </tr>'.
'  <tr bgcolor=yellow>'.
'    <td align=center colspan=2>'.
get_string("form_add_comment_howwastheclass","evaluation").

'<table border=1>'.
'  <tr bgcolor=yellow>'.
'    <td align=center><a href=# onclick="clica(0)" class=linkpicture><img src=icons/indiferente'.strval(rand(1,7)).'.gif><br>'.$s1.'<br><input type=radio name=nota value=0 checked></a></td>'.
'    <td align=center><a href=# onclick="clica(1)" class=linkpicture><img src=icons/ruim'.strval(rand(1,5)).'.gif><br>'.$s2.'<br><input type=radio name=nota value=1></a></td>'.
'    <td align=center><a href=# onclick="clica(2)" class=linkpicture><img src=icons/media'.strval(rand(1,3)).'.gif><br>'.$s3.'<br><input type=radio name=nota value=2></a></td>'.
'    <td align=center><a align=center><a href=# onclick="clica(3)" class=linkpicture><img src=icons/boa'.strval(rand(1,3)).'.gif><br>'.$s4.'<br><input type=radio name=nota value=3></a></td>'.
'    <td align=center><a align=center><a href=# onclick="clica(4)" class=linkpicture><img src=icons/otima'.strval(rand(1,4)).'.gif><br>'.$s5.'<br><input type=radio name=nota value=4></a></td>'.
'    <td align=center><a href=# onclick="clica(5)" class=linkpicture><img src=icons/excelente'.strval(rand(1,4)).'.gif><br>'.$s6.'<br><input type=radio name=nota value=5></a></td>'.
'  </tr>'.
'</table>'.
'         </td>'.
'  </tr>'.
'  <tr><td colspan=2 align=center><input type=submit value="'.
get_string("send_evaluation", "evaluation").
'"'. 'onclick="javascript:return confirm(\''.
get_string("confirm_send_evaluation", "evaluation").
'\')"></td></tr>'.
'<tr><td align=center><font size=1>'.
get_string("images_citation","evaluation").
'</font></td></tr>'.
'</table>'.
'</form>';

  return $str_form;
}

function evaluation_add_comment($evaluation, $course, $user, $comments, $grade)
{
  $message = "";

  $sql="insert into mdl_evaluation_comments (evaluation, course, user, comment, grade, created) values ($evaluation, $course, $user, '$comments',$grade, now())";
  $result = mysql_query($sql) or die(get_string('error_inserting_comment','evaluation').$sql.mysql_error());
  $message = get_string('evaluation_sent','evaluation');

  return $message;
}

function evaluation_show_history($a, $canseewho) {

    $s1 = get_string("dont_know", "evaluation");
    $s2 = get_string("bad", "evaluation");
    $s3 = get_string("regular", "evaluation");
    $s4 = get_string("good", "evaluation");
    $s5 = get_string("great", "evaluation");
    $s6 = get_string("excelent", "evaluation");

    $X = '<h2 align=center>'.get_string('history_title','evaluation').'</h2>';

    $sql="SELECT c4.*, u.username FROM mdl_evaluation_comments c4, mdl_user u where (c4.evaluation=".$a.") and (u.id = c4.user) order by c4.created desc";

    $result = mysql_query($sql) or die(get_string('error_selecting_history','evaluation').mysql_error());

    $i = 0;
    while ($row = mysql_fetch_array($result))
    {
        if ($i == 0)
        {
            $X=$X."<table border=3 bordercolor=green align=center class=body>";
            $X=$X."<tr><td><b>".
            get_string('comments','evaluation').
            "</b></td><td><b>".
            get_string('howwastheclass','evaluation').
            "</b></td><td>".
            get_string('when','evaluation').
            "</td>";
	    if ($canseewho == 1)
            {
              $X=$X."<td>".
              get_string('who','evaluation').
              "</td>";
            }
	    $X=$X."</tr>";
        }
 
        $i++;
        $X=$X."<tr>";

        $X=$X."<td>".nl2br($row['comment'])."</td>";
        $X=$X."<td align=center>";
        if ($row['grade'] == '0') $X=$X.$s1;
        if ($row['grade'] == '1') $X=$X.$s2;
        if ($row['grade'] == '2') $X=$X.$s3;
        if ($row['grade'] == '3') $X=$X.$s4;
        if ($row['grade'] == '4') $X=$X.$s5;
        if ($row['grade'] == '5') $X=$X.$s6;
        $X=$X. "</td>";

        $X=$X. "<td align=center><img src=detail.gif title='".$row['created']."'></td>";

        if ($canseewho == 1)
          $X=$X. "<td align=center><img src=detail.gif title='".$row['username']."'></td>";

        $X=$X. "</tr>";
    }

    if ($i > 0)
        $X=$X. "</table>";
    else
        $X=$X. "<h3 align=center>".get_string('history_empty','evaluation')."</h3>";
    
    return $X;
}

?>
